<template>
	<view class="box">
		<u-search @search="rightClick" :focus="true" :showAction="false" placeholder="日照香炉生紫烟" v-model="keyword"></u-search>
		<view class="search-history">
			<view class="history-title">
				<view>历史搜索</view>
				<view class="removeHistory" @click="removeHistory">
					<u-icon size="28" name="trash"></u-icon>
				</view>
			</view>
			<view class="history-list">
				<text v-for="(item,i) in historyArr" :key="i" @click="historyClick(item)">{{item.content}}</text>
			</view>
		</view>
		<view class="search-hot">

		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//JSON.parse(uni.getStorageSync(('historyArr')))
				historyArr: [],
				keyword: '',
			}
		},
		methods: {
			rightClick() {
				uni.navigateTo({
					url: `searchContent?keyword=${this.keyword}`
				})
				this.getHistoryArr()
				// //历史记录
				// if (!this.keyword) return
				// let index = this.historyArr.indexOf(this.keyword);
				// console.log(index, 'index')
				// if (index < 0) {
				// 	this.historyArr.unshift(this.keyword)
				// 	// uni.setStorageSync('historyArr', JSON.stringify(this.historyArr))
				// } else {
				// 	this.historyArr.unshift(this.historyArr.splice(index, 1)[0])
				// 	// uni.setStorageSync('historyArr', JSON.stringify(this.historyArr))
				// }
			},
			historyClick(item) {
				uni.navigateTo({
					url: `searchContent?keyword=${item.content}`
				})
				console.log(item, 'item')
			},
			removeHistory() {
				let _this = this
				uni.showModal({
					title: "确定删除吗",
					success(e) {
						if (e.confirm) {
							// this.historyArr = []
							// uni.setStorageSync('historyArr', JSON.stringify([]))
							let result = {
								delete_code: "搜索",
								user_id: 2
							}
							_this.http.post('/delete_key_api', result).then(res => {
								_this.getHistoryArr()
							})
						}

					}
				})

			},
			getHistoryArr() {
				let result = {
					province: '河北省',
					city: '保定市',
					district: '望都县',
					user_id: 2
				}
				this.http.post("/search_page_api", result).then(res => {
					this.historyArr = res.result.history
					console.log(this.historyArr, 'this.historyArr')
				})
			}
		},
		onLoad() {
			this.getHistoryArr()
		}
	}
</script>

<style lang="scss" scoped>
	.box {
		// height: 100%;
		padding: 0 20rpx;
	}

	.search-box {
		width: 527rpx;
	}

	.search-history {
		margin-top: 10rpx;

		.history-title {
			display: flex;
			justify-content: space-between;

		}

		.history-list {
			display: flex;
			flex-wrap: wrap;
			align-items: center;

			text {
				padding: 10rpx;
				margin: 10rpx;
				background-color: #ccc;
				color: #fff;
				border-radius: 10rpx;
			}
		}
	}
</style>